package com.hnevc.cnlizhimaomxbc;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.hnevc.cnlizhimaomxbc.util.DB;

import java.util.Map;
import java.util.concurrent.TimeUnit;

public class MainActivity extends AppCompatActivity {
    private SharedPreferences.Editor editor;
    private SharedPreferences sharedPreferences;
    private int remaberFlag = 0;
    private String usernameStr;
    private String passwordetStr;
    private String passwordet ="";
    private CheckBox rememder_cb;
    private EditText number_et ;
    private EditText auth_code_et;
    private Button longin_bt;
    private Button zc_bt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SharedPreferences sharedPreferences=getSharedPreferences("test",MODE_PRIVATE);
        initView();
        initEvent();
        if (sharedPreferences != null){
            String name = sharedPreferences.getString("name","");
            passwordet = sharedPreferences.getString("passwordet","");
            remaberFlag = sharedPreferences.getInt("rememder_cb",0);
            number_et.setText(name);
        }
        if (remaberFlag == 1){
            rememder_cb.setChecked(true);
            auth_code_et.setText(passwordet);
        }
    }
    private void initView(){
        zc_bt = (Button) findViewById(R.id.zc_bt);
        number_et = (EditText) findViewById(R.id.number_et);
        auth_code_et = (EditText) findViewById(R.id.auth_code_et);
        longin_bt = (Button) findViewById(R.id.longin_bt);
        rememder_cb = (CheckBox)findViewById(R.id.rememder_cb);

    }

    private void initData(){
        usernameStr = number_et.getText().toString() + "";
        passwordetStr = auth_code_et.getText().toString() +"";
    }
    private void initEvent(){
        zc_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, LoginActivity.class);
                startActivityForResult(intent,1);
            }
        });

        longin_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initData();
                SharedPreferences sharedPreferences = getSharedPreferences("test",MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                DB db = new DB(MainActivity.this);
                db.loginSelect(usernameStr,passwordet);
                editor.putString("auth_code_et",passwordetStr);
                editor.putString("number_et",usernameStr);
                if (rememder_cb.isChecked()) {
                    remaberFlag = 1;
                    editor.putInt("remeder_flag", remaberFlag);
                    editor.putString("auth_code_et", passwordetStr);

                }else {
                    remaberFlag = 0;
                    editor.putInt("remeder_flag",remaberFlag);
                }
                editor.commit();
                Boolean result = db.loginSelect(usernameStr,passwordetStr);
                if (result) {
                    Intent intent = new Intent(MainActivity.this, HomeActivity.class);
                    startActivity(intent);
                    Toast.makeText(MainActivity.this, "登录成功！", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(MainActivity.this,"登录失败！",Toast.LENGTH_LONG).show();
                }
            }
    });

}
}